﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxRoundPanel;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;
using DevExpress.Web.ASPxTreeView;
using EntryExit.Utils;

namespace EntryExit
{
    public partial class Behaviours : BasePage
    {
        public Employee SelectedEmployee { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += Behaviours_MasterTreeViewNodeSelected;
            }
        }

        void Behaviours_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            DateTime startDate = Convert.ToDateTime(string.Format("{0}/1/{1}", ddlMonth.SelectedIndex + 1, ddlYear.SelectedValue));
            //DateTime endDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}",
            //    DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1), ddlMonth.SelectedIndex + 1, ddlYear.SelectedValue));

            //DateTime endDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}",
            //    ddlMonth.SelectedIndex + 1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1), ddlYear.SelectedValue));

            DateTime endDate = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1));

            ShowMonthlyPerformance(startDate, endDate, SelectedEmployee);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    FillYear();

                    SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);

                    DateTime startDate = Convert.ToDateTime(string.Format("{0}/1/{1}", ddlMonth.SelectedIndex + 1, ddlYear.SelectedValue));
                    //DateTime endDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}",
                    //    DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1), ddlMonth.SelectedIndex + 1, ddlYear.SelectedValue));

                    //DateTime endDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}",
                    //  ddlMonth.SelectedIndex + 1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1), ddlYear.SelectedValue));

                    DateTime endDate = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1));

                    ShowMonthlyPerformance(startDate, endDate, SelectedEmployee);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Page :Behaviours.aspx.cs, Event : Page_Load", ex);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('An internal error occured. Reload the tool and try again. If issue still persists, please report to your administrator..!');", true);
            }
        }

        private void FillYear()
        {
            ddlYear.Items.Clear();
            int maxYear = Convert.ToInt32(ConfigMgr.Instance.GetValue(ConfigKeys.CNFG_MAX_YEAR));
            int minYear = Convert.ToInt32(ConfigMgr.Instance.GetValue(ConfigKeys.CNFG_MIN_YEAR));
            for (int i = minYear; i <= maxYear; i++)
            {
                ddlYear.Items.Add(Convert.ToString(i)); 
            }
            ddlYear.SelectedValue = Convert.ToString(DateTime.Now.Year);
            ddlMonth.SelectedIndex = DateTime.Now.Month - 1;
        }

        private void ShowMonthlyPerformance(DateTime startdate, DateTime enddate, Employee selEmp)
        {
            ParentPanel.HeaderText = "Behaviours : " + selEmp.EmployeeName;
            chartBehaviours.Series[0].Points.Clear();
            Dictionary<string, TimeSpan> loopAttendanceInfo;
            bool shouldShowChart = false;

            DataTable loopDataTable = new DataTable();

            loopDataTable.Columns.Add("Date");
            loopDataTable.Columns.Add("Time", Type.GetType("System.Decimal"));

            int veryLowHours = 0;
            int lowHours = 0;
            int normalHours = 0;
            int overHours = 0;

            EmployeeManagerContext.GetBehaviourReport(startdate, enddate, selEmp.CardHolderId , ref veryLowHours, ref lowHours, ref normalHours, ref overHours);

            chartBehaviours.Series[0].Points.Add(new SeriesPoint("Over Working Hours", overHours));
            chartBehaviours.Series[0].Points.Add(new SeriesPoint("Low Working Hours", lowHours));
            chartBehaviours.Series[0].Points.Add(new SeriesPoint("Normal Working Hours", normalHours));
            chartBehaviours.Series[0].Points.Add(new SeriesPoint("Less Than Three Hours", veryLowHours));


            if ((veryLowHours + lowHours + normalHours + overHours) > 0)
            {
                shouldShowChart = true;
            }

            if (shouldShowChart)
            {
                chartBehaviours.Visible = true;
                lblStatus.Visible = false;
            }
            else
            {
                chartBehaviours.Visible = false;
                lblStatus.Visible = true;
            }
        }

        protected void btnShowBehaviours_Click(object sender, EventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            DateTime startDate = Convert.ToDateTime(string.Format("{0}/1/{1}", ddlMonth.SelectedIndex + 1, ddlYear.SelectedValue));
            //DateTime endDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}",
            //    DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1), ddlMonth.SelectedIndex + 1, ddlYear.SelectedValue));

            //DateTime endDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}",
            //    ddlMonth.SelectedIndex + 1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1), ddlYear.SelectedValue));

            DateTime endDate = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1, DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), ddlMonth.SelectedIndex + 1));

            ShowMonthlyPerformance(startDate, endDate,SelectedEmployee);
        }
    }
}